package com.matt.service.impl;

import com.matt.domain.ChildrenLeaveRequest;
import com.matt.repository.ChildrenLeaveRequestRepository;
import com.matt.service.ChildrenLeaveRequestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/7/13
 * *******************************
 */
@Service
@Slf4j
public class ChildrenLeaveRequestServiceImpl implements ChildrenLeaveRequestService {

    @Autowired
    private ChildrenLeaveRequestRepository childrenLeaveRequestRepository;


    @Override
    public void addChildrenLeaveRequest(ChildrenLeaveRequest childrenMemorandum) {
        childrenLeaveRequestRepository.save(childrenMemorandum);
    }

    @Override
    public ChildrenLeaveRequest getChildrenLeaveRequestById(String id) {
        return childrenLeaveRequestRepository.getOne(id);
    }

    @Override
    public void delete(String id) {
        childrenLeaveRequestRepository.deleteById(id);
    }

    @Override
    public Page<ChildrenLeaveRequest> selChildrenLeaveRequest(String aid, Date startTime, String chldrenId, String classesId, String name, String leaveType, String type, Integer pageNum, Integer pageSize, List<String> collect) {


        // 使用 Specification 构建动态查询条件
        Specification<ChildrenLeaveRequest> specification = (root, query, criteriaBuilder) -> {
            // 创建一个空的 Predicate 列表，用于存储查询条件
            List<Predicate> predicates = new ArrayList<>();

            // 判断 startTime 是否为空，不为空则添加到查询条件中
            if (startTime != null) {
                // 获取当天的起始时间（2025-03-01 00:00:00）
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(startTime);
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                Date startOfDay = calendar.getTime();

                // 获取当天的结束时间（2025-03-01 23:59:59）
                calendar.set(Calendar.HOUR_OF_DAY, 23);
                calendar.set(Calendar.MINUTE, 59);
                calendar.set(Calendar.SECOND, 59);
                Date endOfDay = calendar.getTime();

                // 添加日期范围查询条件
                predicates.add(criteriaBuilder.between(root.get("startTime"), startOfDay, endOfDay));
            }

            // 判断 classesId 是否为空，不为空则添加到查询条件中
            if (chldrenId != null && !chldrenId.isEmpty()) {
                predicates.add(criteriaBuilder.equal(root.get("chldrenId"), chldrenId));
            }

            // 关键调整2：班级条件 - 优先使用前端传的classesId，未传则用collect集合
            if (classesId != null && !classesId.trim().isEmpty()) {
                // 前端传了有效classesId，优先用等值查询
                predicates.add(criteriaBuilder.equal(root.get("classesId"), classesId));
            } else if (!collect.isEmpty()) {
                // 前端未传classesId，但有用户所在班级集合，用in查询
                predicates.add(root.get("classesId").in(collect));
            }

            // 判断 leaveType 是否为空，不为空则添加到查询条件中
            if (leaveType != null && !leaveType.isEmpty()) {
                predicates.add(criteriaBuilder.equal(root.get("leaveType"), leaveType));
            }

            // 判断 leaveType 是否为空，不为空则添加到查询条件中
            if (type != null && !type.isEmpty()) {
                predicates.add(criteriaBuilder.equal(root.get("type"), type));
            }


            // 判断 name 是否为空，不为空则添加到查询条件中
            if (name != null && !name.isEmpty()) {
                predicates.add(criteriaBuilder.like(root.get("name"), "%" + name + "%"));
            }

            // 判断 name 是否为空，不为空则添加到查询条件中
            if (aid != null && !aid.isEmpty()) {
                predicates.add(criteriaBuilder.equal(root.get("aid"), aid));
            }

            // 将多个条件用 AND 连接
            return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
        };

        Sort sort = Sort.by(Sort.Order.desc("createTime"));

        Page<ChildrenLeaveRequest> all = childrenLeaveRequestRepository.findAll(specification, PageRequest.of(pageNum, pageSize, sort));
        return all;
    }

    @Override
    public Page<ChildrenLeaveRequest> selChildrenLeaveRequest1(String aid, Date startTime, String chldrenId, String classesId, String name, String leaveType, String type, Integer pageNum, Integer pageSize, List<String> collect, Integer isShow) {
        // 使用 Specification 构建动态查询条件
        Specification<ChildrenLeaveRequest> specification = (root, query, criteriaBuilder) -> {
            // 创建一个空的 Predicate 列表，用于存储查询条件
            List<Predicate> predicates = new ArrayList<>();

            // 判断 startTime 是否为空，不为空则添加到查询条件中
            if (startTime != null) {
                // 获取当天的起始时间（2025-03-01 00:00:00）
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(startTime);
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                Date startOfDay = calendar.getTime();

                // 获取当天的结束时间（2025-03-01 23:59:59）
                calendar.set(Calendar.HOUR_OF_DAY, 23);
                calendar.set(Calendar.MINUTE, 59);
                calendar.set(Calendar.SECOND, 59);
                Date endOfDay = calendar.getTime();

                // 添加日期范围查询条件
                predicates.add(criteriaBuilder.between(root.get("startTime"), startOfDay, endOfDay));
            }

            // 判断 classesId 是否为空，不为空则添加到查询条件中
            if (chldrenId != null && !chldrenId.isEmpty()) {
                predicates.add(criteriaBuilder.equal(root.get("chldrenId"), chldrenId));
            }

            // 关键调整2：班级条件 - 优先使用前端传的classesId，未传则用collect集合
            if (classesId != null && !classesId.trim().isEmpty()) {
                // 前端传了有效classesId，优先用等值查询
                predicates.add(criteriaBuilder.equal(root.get("classesId"), classesId));
            } else if (!collect.isEmpty()) {
                // 前端未传classesId，但有用户所在班级集合，用in查询
                predicates.add(root.get("classesId").in(collect));
            }

            if (isShow!=null&&isShow==1){
                predicates.add(criteriaBuilder.notEqual(root.get("state"), 3));
            }
            // 判断 leaveType 是否为空，不为空则添加到查询条件中
            if (leaveType != null && !leaveType.isEmpty()) {
                predicates.add(criteriaBuilder.equal(root.get("leaveType"), leaveType));
            }

            // 判断 leaveType 是否为空，不为空则添加到查询条件中
            if (type != null && !type.isEmpty()) {
                predicates.add(criteriaBuilder.equal(root.get("type"), type));
            }


            // 判断 name 是否为空，不为空则添加到查询条件中
            if (name != null && !name.isEmpty()) {
                predicates.add(criteriaBuilder.like(root.get("name"), "%" + name + "%"));
            }

            // 判断 name 是否为空，不为空则添加到查询条件中
            if (aid != null && !aid.isEmpty()) {
                predicates.add(criteriaBuilder.equal(root.get("aid"), aid));
            }

            // 将多个条件用 AND 连接
            return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
        };

        Sort sort = Sort.by(Sort.Order.desc("createTime"));

        Page<ChildrenLeaveRequest> all = childrenLeaveRequestRepository.findAll(specification, PageRequest.of(pageNum, pageSize, sort));
        return all;
    }


}
